package com.axjc.gqzr.controller;

import com.axjc.gqzr.common.PageInfo;
import com.axjc.gqzr.common.R;
import com.axjc.gqzr.domain.Gqzr;
import com.axjc.gqzr.domain.SysUser;
import com.axjc.gqzr.service.GqzrService;
import com.axjc.gqzr.service.IFilesService;
import com.axjc.gqzr.util.ApplicationContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * (Gqzr)表控制层
 *
 * @author makejava
 * @since 2024-09-17 14:32:41
 */
@RestController
@RequestMapping("/api/v1/gqzr")

@RequiredArgsConstructor
public class GqzrController {
    /**
     * 服务对象
     */
    private final GqzrService gqzrService;

    private final IFilesService filesService;

    /**
     * 下载单个文件
     */
    @PostMapping("/download")
    public ResponseEntity<Resource> download(long id) throws Exception {
        return filesService.download(id);
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(PageInfo<Gqzr> page, Gqzr gqzr) {
        Page<Gqzr> page1 = this.gqzrService.page(page, new QueryWrapper<>(gqzr));
        return R.ok(page1.getRecords());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.gqzrService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param model 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Gqzr model) {
        model.setId(IdWorker.getId());
        SysUser user = ApplicationContextUtil.GetCurrentUser();
        model.setUser_id(user.getUser_id());
        return R.ok(this.gqzrService.save(model));
    }

    /**
     * 修改数据
     *
     * @param gqzr 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Gqzr gqzr) {
        return R.ok(this.gqzrService.updateById(gqzr));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.gqzrService.removeByIds(idList));
    }
}

